
import 'package:erp/Entity/ProductDetailAtPurchaseOrSell.dart';
import 'package:erp/Entity/SalesDetailsProductGroup.dart';
import 'package:erp/Entity/SalesOrder.dart';
import 'package:erp/Events/EventAddrs.dart';
import 'package:erp/Store/SalesOrderStore.dart';
import 'package:erp/UI/CodeResources/Localization.dart';
import 'package:erp/UI/Components/EmbeddableStatefulWidget.dart';
import 'package:erp/UI/Components/HeroLogo.dart';
import 'package:erp/UI/Components/KeyValueView.dart';
import 'package:erp/UI/Components/ListDividerGenerator.dart';
import 'package:erp/UI/Components/LogoScaffold.dart';
import 'package:erp/UI/Components/MessageDialog.dart';
import 'package:erp/global.dart';
import 'package:flutter/material.dart';

class SalesDetailsPage extends StatelessWidget {
  SalesOrder order;

  SalesDetailsPage(this.order) : assert (order != null){

  }

  @override
  Widget build(BuildContext context) {
    return EmbeddedWidget(
      initEvent: (state){
        state.eventIds = [
          globalBus.subscribe(Events.SALES_ORDER_DETAIL_SELECT_SUCCESS, (bus, data){state.setState((){});}),
          globalBus.subscribe(Events.SALES_ORDER_DETAIL_SELECT_ERROR, (bus, error) async {
            await showMessageDialog(context, title: "ئوقۇشتا خاتالىق كۆرۈلدى", content: error.toString());
          }),
        ];

        if((order.products?.length ?? 0) == 0) globalBus.publish(Events.SALES_ORDER_DETAIL_SELECT_REQUEST, order);
      },
      widgetBuilder: (context, state){
      var currentLocale = AppLocalizations.of(context);
      return Directionality(textDirection: currentLocale.textDirection, child: Scaffold(
        floatingActionButton: FloatingActionButton(
            heroTag: "FloatingActionButton",
            child: Icon(Icons.close),
            onPressed: (){
              Navigator.of(context).pop();
            }
        ),
        body: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
              expandedHeight: 320.0,
              flexibleSpace: FlexibleSpaceBar(
                title: Text("سىتىش تالونى تەپسىلاتى"),
                centerTitle: true,
                background: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    HeroLogo(assetsLogoName: "assets/pictures/cells.png", heroName: order.orderNumber),
                    SizedBox(height: 10.0,),
                    KeyValueView({
                      "خىرىدار: ": order.customerName ?? "",
                      "بىجىرگۈچى: ": order.employeeName,
                      "تالون نومۇرى: ": order.orderNumber,
                      "ۋاقتى: ": order.time,
                      "ئومومى سوممىسى: ": order.amount.toString(),
                      "ئەمەلىي سوممىسى: ": order.sAmount.toString(),
                    }, textColor: Colors.white),
                    SizedBox(height: 40.0,)
                  ],
                ),
              ),
            ),
            SliverList(
              delegate: SliverChildListDelegate(ListDividerGenerator(SalesOrderStore.orders.singleWhere((item) => item.orderNumber == order.orderNumber).products?.map((productGroup) => buildListItem(productGroup))?.toList() ?? [], Divider()).items),
            )
          ],
        ),
      ));
      }
    );
  }

  Widget buildListItem(ProductDetailAtPurchaseOrSell productGroup){
    return ExpansionTile(
      leading: Image.asset("assets/pictures/box.png", width: 40.0, height: 40.0,),
      title: KeyValueView({
        "ئىسمى: ": productGroup.nameUg,
        "سانى: ": productGroup.count ?? 0.0,
        "كىرىش باھاسى: ": productGroup.inPrice,
        "سىتىش باھاسى: ": productGroup.sellPrice
      }),
      children: <Widget>[
        KeyValueView({
          "تاۋار نومۇرى: ": productGroup.productCode,
          "ئامبار: ": productGroup.warehouseName,
          "تاياقچە كودى: ": productGroup.barCode,
        }, padding: EdgeInsets.symmetric(vertical: 10.0),)
      ],
    );
  }
}
